<?php

namespace App\Console\Commands;

use App\Models\StaticProxyDetail;
use Carbon\Carbon;
use Illuminate\Console\Command;

class StaticProxyExpired extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:staticProxyExpired';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '静态代理过期';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 获取当前时间（精确到秒）
        $currentTime = Carbon::now()->format('Y-m-d H:i:s');
        // 已过期
        $expiredCount = StaticProxyDetail::where('expired_time', '<', $currentTime)->where('status', '!=', 0)->update(['status' => 0]);
        // 计算三天后的时间
        $threeDaysLater = Carbon::now()->addDays(3)->format('Y-m-d H:i:s');
        // 即将过期
        $soonExpiredCount = StaticProxyDetail::where('expired_time', '>', $currentTime)->where('expired_time', '<', $threeDaysLater)->where('status', '!=', 0)->update(['status' => 2]);

        $this->info("已更新 {$expiredCount} 条过期代理，{$soonExpiredCount} 条即将过期代理");
        return true;
    }
}
